package com.hilaver.dzmis.web.servlet.assistntinfo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.accesscontrol.AcUser;
import com.hilaver.dzmis.service.impl.SysMboxServiceImpl;
import com.hilaver.dzmis.sys.SysMbox;
import com.hilaver.dzmis.sys.SysMboxViewer;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class SysMboxServlet extends AbstractBaseServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2165950193570641607L;

	public final static int ACTOIN_SET_CLOSED = 20; // "getAllPagination";
	public final static int ACTION_EDIT_SYS_MBOX_VIEWER = 101;
	public final static int ACTION_DELETE_SYS_MBOX_VIEWER = 102;
	public final static int ACTION_GET_SYS_MBOX_VIEWER = 103;
	public final static int ACTION_GET_ALL_FROM_MBOX = 105;
	public final static int ACTION_GET_MY_MSG_PAGENATION = 106;
	public final static int ACTION_GET_MSG_UNCONFIRMED_PAGENATION = 107;
	
	private SysMboxServiceImpl sysMboxService;
	

	public SysMboxServlet(){
		this.sysMboxService = new SysMboxServiceImpl();
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			writer.write(execute(request));
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}
	
	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.sysMboxService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, this.sysMboxService);
		
		String rtn = "";
		String isClosed = (String)this.getValue(request, "isClosed");
		String ctOperation = (String)this.getValue(request, "ctOperation");
		
		switch (this.action) {
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination";
			this.setFilterParam(request);
			this.setPaginationParam(request);
			
			if(this.filters == null){
				this.filters = new String[0];
			}
			
			List<String> filterList = new ArrayList<String>();
			for (int i = 0; i < this.filters.length; i++) {
				filterList.add(this.filters[i]);
			}
			if(!StringUtils.isEmpty(ctOperation)){
				filterList.add("ctOperation='" + ctOperation + "'");
			}
			this.filters = filterList.toArray(new String[0]);
			
			rtn = this.sysMboxService.getAllPagination(this.page, this.sort, this.dir, this.filters);
			break;
		case ACTION_GET_MSG_UNCONFIRMED_PAGENATION:
			this.setFilterParam(request);
			this.setPaginationParam(request);
			
			if(this.filters == null){
				filters = new String[]{"isClosed=" + isClosed};
			}else{
				String[] temp = new String[filters.length + 1];
				temp[0] = "isClosed=" + isClosed;
				for(int i = 1; i < temp.length; i ++){
					temp[i] = this.filters[i - 1];
				}
				this.filters = temp;
			}
			
			rtn = this.sysMboxService.getAllUnconfirmedPagination(this.page, this.sort, this.dir, this.filters);
			break;
		case ACTION_GET_WITH_DESCRIPTION:
			super.setId(request);
			rtn = this.sysMboxService.get(this.id);
			break;
		case ACTION_EDIT_SYS_MBOX_VIEWER:
			super.setId(request);
			rtn = this.sysMboxService.editSysMboxViewers(fillObjs(request));
			break;
		case ACTION_GET_ALL_FROM_MBOX:
			super.setId(request);
			super.setFilterParam(request);
			
			if(this.filters == null){
				filters = new String[]{"sysMbox.id=" + this.id};
			}else{
				String[] temp = new String[filters.length + 1];
				temp[0] = "sysMbox.id=" + this.id;
				for(int i = 1; i < temp.length; i ++){
					temp[i] = this.filters[i - 1];
				}
				this.filters = temp;
			}
			
			rtn = this.sysMboxService.getAllViewer(this.sort, this.dir, this.filters);
			break;
		case ACTION_GET_SYS_MBOX_VIEWER:
			super.setId(request);
			rtn = this.sysMboxService.getViewer(this.id);
			break;
		case ACTION_DELETE_SYS_MBOX_VIEWER:
			super.setId(request);
			rtn = this.sysMboxService.deleteViewer(this.id);
			break;
		case ACTOIN_SET_CLOSED:
			super.setId(request);
			rtn = this.sysMboxService.setClosed(this.id);
			break;
		case ACTION_GET_MY_MSG_PAGENATION:
			super.setId(request);
			super.setFilterParam(request);
			super.setPaginationParam(request);
			rtn = this.sysMboxService.getMyViewerPagination(this.page, this.sort, this.dir, this.filters);
			break;
		}
		return rtn;
	}
	
	protected SysMboxViewer[] fillObjs(HttpServletRequest request) throws Exception{
		super.fillObj(SysMboxViewer.class.getName(), request);
		
		String msgIdStr = (String)this.getValue(request, "msgId");
		Integer msgId = Integer.parseInt(msgIdStr);
		String[] receiverIds = ((String)this.getValue(request, "receiverIds")).split(",");
		SysMboxViewer[] smvs = new SysMboxViewer[receiverIds.length];
		SysMboxViewer smv = (SysMboxViewer)this.obj;
		for (int i = 0; i < receiverIds.length; i ++) {
			Integer receiverId = Integer.parseInt(receiverIds[i]);
			AcUser receiver = new AcUser();
			receiver.setId(receiverId);
			SysMbox sm = new SysMbox();
			sm.setId(msgId);
			
			SysMboxViewer newOne = smv.clone();
			newOne.setReceiver(receiver);
			newOne.setSysMbox(sm);
			smvs[i] = newOne;
		}
		return smvs;
	}
	
}
